﻿using GDGeek;
using Microsoft.MixedReality.Toolkit.Utilities;
using MrPP.UX;
using MrPP.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class ButtonList : MonoBehaviour
    {
        [SerializeField]
        private GridObjectCollection _grid;
        [SerializeField]
        private Button _phototype;
        private List<Button> list_ = null;

        void Start() {
            
            if (CabinetGroup.Instance.initialized) {
                refresh();
            }
            CabinetGroup.Instance.onSetup += refresh;
        }

        private void refresh()
        {
            //Debug.LogError("b");
            _setup(CabinetGroup.Instance.count, CabinetGroup.Instance.fix);
        }

        void OnDestroy() {
            if (CabinetGroup.IsInitialized) {

                CabinetGroup.Instance.onSetup -= refresh;
            }
        }
        public void clear() {
            if (list_ != null) { 
                foreach (var b in list_)
                {
                    GameObject.DestroyImmediate(b.gameObject);
                }
                list_.Clear();
            }
        }
        public void _setup(int n, int fixedIndex)
        {

            clear();
            for (int i = 0; i < n; ++i)
            {
                Button button = GameObject.Instantiate(_phototype, this.transform, true);
                button.name = _phototype.name + "_" + i.ToString();
                button.gameObject.SetActive(true);

            }

            _grid.UpdateCollection();
            Button[] buttons = this.gameObject.GetComponentsInChildren<Button>();
            TaskWait tw = new TaskWait(0);
            TaskManager.PushBack(tw, delegate {

                list_ = new List<Button>(buttons);
                list_.Sort((Button left, Button right) =>
                {
                    Debug.Log("x:" + left.transform.localPosition.x + ":" + left.gameObject.name);
                    if (left.transform.localPosition.x < right.transform.localPosition.x)
                    {
                        return -1;
                    }
                    else if (left.transform.localPosition.x == right.transform.localPosition.x)
                    {
                        return 0;
                    }
                    return 1;

                });
                for (int i = 0; i < list_.Count; ++i)
                {
                    var l = list_[i];
                    IntId ii = l.gameObject.AskComponent<IntId>();
                    ii.value = i;
                    Debug.Log(l.name + ":" + l.transform.localPosition.x);
                    
                    if (i == fixedIndex)
                    {
                        l.state = Button.State.Disable;
                        l.text = "固定列";
                    }
                    else {
                        l.text = "第" + (i+1).ToString() + "列";
                    }
                    l.refresh();
                    

                }
            });
            TaskManager.Run(tw);
        }

        public Button getButton(int selected)
        {
            return list_[selected];
        }

       
    }
}